package com.lhj.bootdemo.designmode.proxy.jdk;

import java.lang.reflect.Proxy;

/**
 * @author lhj
 * @version 1.0
 * @date 2022/5/18 9:15 上午
 * @description:
 */

public class JdkDynamic0bjectFactory {


    public static <T> T getProxiedObject(Class clazz) {

        try {
            JdkProxy proxy = new JdkProxy(clazz.newInstance());
            //JDK动态代理只能针对实现了接口的类进行代理，newProxyInstance函数所需参数就可看出
            T proxied = (T) Proxy.newProxyInstance(JdkDynamic0bjectFactory.class.getClassLoader(), clazz.getInterfaces(), proxy);
            return proxied;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
